package artid.com.arttogo.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.Date;

public class Config {

	public static boolean isDebug = true;
	public static boolean isLogSave = true;
	public static final String TAG = "Config";

	public static void e(Object msg) {
		if (isDebug)
			Log.e(getTag(), msg == null ? "null" : msg.toString());
	}

	public static void d(Object msg) {
		if (isDebug)
			Log.d(getTag(), msg == null ? "null" : msg.toString());
	}

	public static void i(Object msg) {
		if (isDebug)
			Log.i(getTag(), msg == null ? "null" : msg.toString());
	}

	private static File logFile;
	private static Date logDate;

//	public static void s(Object msg) {
//		if (isLogSave) {
//			if (null == logFile) {
//				File dir = FileUtil.getSdcardDirectory("log");
//				logFile = new File(dir.getPath(), "log.txt");
//				logDate = new Date();
//			}
//			logDate.setTime(System.currentTimeMillis());
//			StringBuilder sb = new StringBuilder();
//			sb.append(logDate.toString()).append(' ');
//			sb.append(getTag()).append(' ');
//			sb.append(msg == null ? "null" : msg.toString()).append('\n');
//			FileUtil.saveBytesToFile(sb.toString().getBytes(), logFile, true);
//		}
//	}

	public static void showTip(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private static Toast toast;
	private static int mScreenWidth;
	private static int AIRPLAY_MESSAGE_HIDE_TOAST = 1;


	public static void cancelToast() {
		if (toast != null) {
			toast.cancel();
		}
	}/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */

	private static Handler m_Handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				cancelToast();
				break;

			}
		}
	};

	public static String getTag() {
		StackTraceElement ste = Thread.currentThread().getStackTrace()[4];
		StringBuilder sb = new StringBuilder();
		sb.append(getSimpleClassName(ste.getClassName()));
		sb.append('.');
		sb.append(ste.getMethodName());
		sb.append('(');
		sb.append(ste.getLineNumber());
		sb.append(')');
		return sb.toString();
	}

	public static String getSimpleClassName(String path) {
		int index = path.lastIndexOf('.');
		if (index < 0) {
			return path;
		} else {
			return path.substring(index + 1);
		}
	}
}
